package com.common.baselibrary.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.common.baselibrary.BaseLibraryApplication;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;

/**
 * Created by xuyougen
 * 2019/10/31.
 */
public class NetworkHelper {

    public enum NetType {
        OFFLINE, MOBILE, WIFI, UNKNOWN
    }

    /**
     * 获取当前网络类型
     *
     * @return {@link NetType} 网络类型
     */
    public static NetType getNetworkType() {
        if (!isNetworkConnected())
            return NetType.OFFLINE;
        ConnectivityManager manager = (ConnectivityManager) BaseLibraryApplication.getBaseApplication()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info != null)
            switch (info.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    return NetType.WIFI;
                case ConnectivityManager.TYPE_MOBILE:
                    return NetType.MOBILE;
            }
        return NetType.UNKNOWN;
    }

    /**
     * 获取网络连接状态
     *
     * @return true 已连接互联网
     */
    public static boolean isNetworkConnected() {
        ConnectivityManager manager = (ConnectivityManager) BaseLibraryApplication.getBaseApplication()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null)
            return false;
        NetworkInfo info = manager.getActiveNetworkInfo();
        return info != null && info.isConnected();
    }

    /**
     * 获取当前 ip 地址
     *
     * @param context context
     * @return 当前 ip 地址
     */
    public static String getIPv4Address(Context context) {
        if (!isNetworkConnected())
            return "0.0.0.0";
        if (getNetworkType() == NetType.MOBILE) {
            return getLocalIPv4Address();
        } else if (getNetworkType() == NetType.WIFI) {
            return getWLANIPv4Address(context);
        }
        return "0.0.0.0";
    }

    /**
     * 获取当前 ip 地址(内网ip)
     *
     * @param context
     * @return
     */
    public static String getWLANIPv4Address(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int i = wifiInfo.getIpAddress();
            return int2ip(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0.0.0.0";
    }


    public static String getLocalIPv4Address() {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface netInterface : interfaces) {
                List<InetAddress> addresses = Collections.list(netInterface.getInetAddresses());
                for (InetAddress address : addresses) {
                    if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                        return address.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "0.0.0.0";
    }


    /**
     * 将 ip 的整数形式转换成 String 形式
     *
     * @param ip ip
     * @return ip string
     */
    public static String int2ip(int ip) {
        StringBuilder builder = new StringBuilder();
        builder.append(ip & 0xFF).append(".");
        builder.append((ip >> 8) & 0xFF).append(".");
        builder.append((ip >> 16) & 0xFF).append(".");
        builder.append((ip >> 24) & 0xFF);
        return builder.toString();
    }

}